package com.todaynews.frame.theme.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import com.todaynews.R;
import com.todaynews.frame.theme.ThemeHelper;
import com.todaynews.frame.theme.Themed;
import com.todaynews.frame.theme.ui.ThemedIcon;

public class SettingBasic extends FrameLayout implements Themed {

    private final String iconString;
    @StringRes
    private final int titleRes;
    @StringRes
    private final int captionRes;
    ThemedIcon icon;
    TextView title;
    TextView caption;

    public SettingBasic(Context context) {
        this(context, null);
    }

    public SettingBasic(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SettingBasic(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setBackgroundResource(R.drawable.ripple);
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view  =inflater.inflate(R.layout.view_setting_basic, this);
        icon = view.findViewById(R.id.icon);
        title = view.findViewById(R.id.title);
        caption = view.findViewById(R.id.caption);
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.SettingBasic);
        iconString = a.getString(R.styleable.SettingBasic_settingIcon);
        titleRes = a.getResourceId(R.styleable.SettingBasic_settingTitle, 0);
        captionRes = a.getResourceId(R.styleable.SettingBasic_settingCaption, 0);
        int minimumApi = a.getInteger(R.styleable.SettingBasic_settingMinApi, 0);
        a.recycle();

        if (Build.VERSION.SDK_INT < minimumApi) setVisibility(GONE);
    }

    @Override
    protected void onFinishInflate() {
        icon.setIcon(icon.getIcon().icon(iconString));
        title.setText(titleRes);
        caption.setText(captionRes);
        /*
        setPadding((int) getResources().getDimension(R.dimen.medium_spacing), 0, (int) getResources().getDimension(R.dimen.medium_spacing), 0);
        setMinimumHeight((int) getResources().getDimension(R.dimen.listitem_height_twoline));
        */
        super.onFinishInflate();
    }

    @Override
    public void refreshTheme(ThemeHelper themeHelper) {

    }

}
